//
//  ServiceSubscribe.m
//  suRead
//
//  Created by lidi on 2018/11/28.
//  Copyright © 2018 bao. All rights reserved.
//

#import "ServiceSubscribe.h"
#import "M_News.h"
#import "M_Subscribe.h"
#import "M_SubscribeInfo.h"

@implementation ServiceSubscribe

+ (NSURLSessionDataTask *)doFetchServiceList:(NSString *)userid_ pageindex:(NSInteger)pageindex_ pagesize:(NSInteger)pagesize_ success:(void (^)(id _Nonnull, NSString * _Nonnull))success_ failure:(void (^)(NSString * _Nonnull))failure_ {
    
    NSString *uid = @"";
    if (userid_ && [userid_ isKindOfClass:[NSString class]]) {
        uid = userid_;
    } else {
        uid = @"";
    }
    
    NSString *urlString = [NSString stringWithFormat:@"%@&userid=%@&pageindex=%ld&pagesize=%ld",url_service_list,uid,pageindex_,pagesize_];
    
    return [[ServiceUtils commonHTTPManager] GET:urlString parameters:nil progress:^(NSProgress * _Nonnull uploadProgress) {
        
    } success:^(NSURLSessionDataTask *task, id responseObject) {
        
        NSMutableDictionary *resDic = [NSMutableDictionary dictionary];
        
        if (responseObject && [responseObject isKindOfClass:[NSArray class]]) {
            for (NSDictionary *itemDic in responseObject) {
                
                NSString *type = [itemDic objectForKey:@"type"];
                if ([type isEqualToString:@"focus"]) {
                    //焦点图
                    NSArray *dataArray = [itemDic objectForKey:@"item"];
                    NSArray *focus = [MTLJSONAdapter modelsOfClass:[M_News class] fromJSONArray:dataArray error:nil];
                    if (!focus) {
                        focus = @[];
                    }
                    
                    [resDic setObject:focus forKey:@"focus"];
                }
                
                if ([type isEqualToString:@"list"]) {
                    NSError *error;
                    NSArray *dataArray = [itemDic objectForKey:@"item"];
                    NSArray *list = [MTLJSONAdapter modelsOfClass:[M_News class] fromJSONArray:dataArray error:&error];
                    if (!list) {
                        list = @[];
                    }
                    [resDic setObject:list forKey:@"list"];
                    
                    NSArray *service_item = [itemDic objectForKey:@"service_item"];
                    if (service_item && [service_item isKindOfClass:[NSArray class]] && [service_item count] > 0) {
                        [resDic setObject:service_item forKey:@"service_item"];
                    }
                }
            }
        }else if (responseObject && [responseObject isKindOfClass:[NSDictionary class]]){
            NSError *error;
            NSArray *dataArray = [responseObject objectForKey:@"list"];
            NSArray *list = [MTLJSONAdapter modelsOfClass:[M_News class] fromJSONArray:dataArray error:&error];
            if (!list) {
                list = @[];
            }
            [resDic setObject:list forKey:@"list"];
        }
        success_(resDic,@"获取成功");
        
    } failure:^(NSURLSessionDataTask *task, NSError *error) {
        if (failure_) {
            failure_(@"网络不给力");
        }
    }];
}

+ (NSURLSessionDataTask *)doFetchClassify:(void (^)(id responseObject,NSString *message))success_
                                  failure:(void (^)(NSString *message))failure_ {
    
    return [[ServiceUtils commonHTTPManager] GET:url_classify parameters:nil progress:^(NSProgress * _Nonnull uploadProgress) {
        
    } success:^(NSURLSessionDataTask *task, id responseObject) {
        if (responseObject && [responseObject isKindOfClass:[NSDictionary class]]){
            NSArray *dataArray = [responseObject objectForKey:@"item"];
            if (success_) {
                success_(dataArray,@"获取成功");
            }
        } else {
            if (failure_) {
                failure_(@"暂无分类信息");
            }
        }
    } failure:^(NSURLSessionDataTask *task, NSError *error) {
        if (failure_) {
            failure_(@"网络不给力");
        }
    }];
}

+ (NSURLSessionDataTask *)doFetchServicePidList:(NSInteger)pid_
                                         userid:(NSString *)userid_
                                      pageindex:(NSInteger)pageindex_
                                       pagesize:(NSInteger)pagesize_
                                        success:(void (^)(id responseObject,NSString *message))success_
                                        failure:(void (^)(NSString *message))failure_ {
    
    NSString *uid = @"";
    if (userid_ && [userid_ isKindOfClass:[NSString class]]) {uid = userid_;}
    
    NSString *urlString = [NSString stringWithFormat:@"%@&pid=%ld&userid=%@&pageindex=%ld&pagesize=%ld",url_service_pid_list,pid_,uid,pageindex_,pagesize_];
    
    return [[ServiceUtils commonHTTPManager] GET:urlString parameters:nil progress:^(NSProgress * _Nonnull uploadProgress) {
        
    } success:^(NSURLSessionDataTask *task, id responseObject) {
        if (responseObject && [responseObject isKindOfClass:[NSDictionary class]]){
            NSError *error;
            NSArray *dataArray = [responseObject objectForKey:@"item"];
            NSArray *list = [MTLJSONAdapter modelsOfClass:[M_Subscribe class] fromJSONArray:dataArray error:&error];
            if (!list) {
                list = @[];
            }
            success_(list,@"获取成功");
        } else {
            if (failure_) {
                failure_(@"当前无数据");
            }
        }        
    } failure:^(NSURLSessionDataTask *task, NSError *error) {
        if (failure_) {
            failure_(@"网络不给力");
        }
    }];
}

+ (NSURLSessionDataTask *)doFetchServiceWidList:(NSInteger)wid_
                                         userid:(NSString *)userid_
                                      pageindex:(NSInteger)pageindex_
                                       pagesize:(NSInteger)pagesize_
                                        success:(void (^)(id responseObject,NSString *message))success_
                                        failure:(void (^)(NSString *message))failure_ {
    NSString *uid = @"";
    if (userid_ && [userid_ isKindOfClass:[NSString class]]) {
        uid = userid_;
    } else {
        uid = @"";
    }
    
    NSString *urlString = [NSString stringWithFormat:@"%@&wid=%ld&userid=%@&pageindex=%ld&pagesize=%ld",url_sevice_wid_list,wid_,uid,pageindex_,pagesize_];
    
    return [[ServiceUtils commonHTTPManager] GET:urlString parameters:nil progress:^(NSProgress * _Nonnull uploadProgress) {
        
    } success:^(NSURLSessionDataTask *task, id responseObject) {
        
        NSMutableDictionary *res = [NSMutableDictionary dictionary];
        if (responseObject && [responseObject isKindOfClass:[NSArray class]] && [responseObject count] > 0){
            
            NSDictionary *data = [responseObject objectAtIndex:0];
            
            NSError *error;
            NSArray *dataArray = [data objectForKey:@"item"];
            NSArray *list = [MTLJSONAdapter modelsOfClass:[M_News class] fromJSONArray:dataArray error:&error];
            if (!list) {
                list = @[];
            }
            [res setObject:list forKey:@"item"];
            
            NSDictionary *info = [data objectForKey:@"service_data"];
            M_SubscribeInfo *subInfo = [MTLJSONAdapter modelOfClass:[M_SubscribeInfo class] fromJSONDictionary:info error:&error];
            if (subInfo) {
                [res setObject:subInfo forKey:@"info"];
            }
            success_(res,@"获取成功");
        } else {
            success_(@{@"list":@[]},@"获取成功");
        }
    } failure:^(NSURLSessionDataTask *task, NSError *error) {
        if (failure_) {
            failure_(@"网络不给力");
        }
    }];
    
    
}

+ (NSURLSessionDataTask *)doFetchServiceUidList:(NSString *)userid_
                                      pageindex:(NSInteger)pageindex_
                                       pagesize:(NSInteger)pagesize_
                                        success:(void (^)(id responseObject,NSString *message))success_
                                        failure:(void (^)(NSString *message))failure_ {
    NSString *uid = @"";
    if (userid_ && [userid_ isKindOfClass:[NSString class]]) {uid = userid_;}
    
    NSString *urlString = [NSString stringWithFormat:@"%@&userid=%@&pageindex=%ld&pagesize=%ld",url_service_uid_list,uid,pageindex_,pagesize_];
    
    return [[ServiceUtils commonHTTPManager] GET:urlString parameters:nil progress:^(NSProgress * _Nonnull uploadProgress) {
        
    } success:^(NSURLSessionDataTask *task, id responseObject) {
        if (responseObject && [responseObject isKindOfClass:[NSDictionary class]]){
            NSError *error;
            NSArray *dataArray = [responseObject objectForKey:@"item"];
            NSArray *list = [MTLJSONAdapter modelsOfClass:[M_Subscribe class] fromJSONArray:dataArray error:&error];
            if (!list) {
                list = @[];
            }
            success_(list,@"获取成功");
        } else {
            if (failure_) {
                failure_(@"当前无数据");
            }
        }
    } failure:^(NSURLSessionDataTask *task, NSError *error) {
        if (failure_) {
            failure_(@"网络不给力");
        }
    }];
}

+ (NSURLSessionDataTask *)doServiceSubscribe:(NSInteger)wid_
                                      userid:(NSString *)userid_
                                     success:(void (^)(id responseObject,NSString *message))success_
                                     failure:(void (^)(NSString *message))failure_ {
    NSString *uid = @"";
    if (userid_ && [userid_ isKindOfClass:[NSString class]]) {
        uid = userid_;
    } else {
        uid = @"";
    }
    
    NSMutableDictionary *params = [NSMutableDictionary dictionary];
    [params setObject:uid forKey:@"userid"];
    [params setObject:@(wid_) forKey:@"wid"];
    
    NSString *urlString = url_service_subscribe;
    return [[ServiceUtils commonHTTPManager] POST:urlString parameters:params progress:^(NSProgress * _Nonnull uploadProgress) {
        
    } success:^(NSURLSessionDataTask *task, id responseObject) {
        success_(@"",@"订阅成功");
    } failure:^(NSURLSessionDataTask *task, NSError *error) {
        if (failure_) {
            failure_(@"网络不给力");
        }
    }];
}

+ (NSURLSessionDataTask *)doServiceUnsubscribe:(NSInteger)wid_
                                        userid:(NSString *)userid_
                                       success:(void (^)(id responseObject,NSString *message))success_
                                       failure:(void (^)(NSString *message))failure_ {
    NSString *uid = @"";
    if (userid_ && [userid_ isKindOfClass:[NSString class]]) {
        uid = userid_;
    } else {
        uid = @"";
    }
    
    NSMutableDictionary *params = [NSMutableDictionary dictionary];
    [params setObject:uid forKey:@"userid"];
    [params setObject:@(wid_) forKey:@"wid"];
    
    NSString *urlString = url_service_unsubscribe;
    return [[ServiceUtils commonHTTPManager] POST:urlString parameters:params progress:^(NSProgress * _Nonnull uploadProgress) {
        
    } success:^(NSURLSessionDataTask *task, id responseObject) {
        success_(@"",@"取消订阅成功");
    } failure:^(NSURLSessionDataTask *task, NSError *error) {
        if (failure_) {
            failure_(@"网络不给力");
        }
    }];
}
@end
